package com.bbzn.device.client.service.emq.order.impl.nb;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.dto.ct.data.ProductUnionDTO;
import com.bbzn.device.client.dto.ct.request.RequestCreateCommand;
import com.bbzn.device.client.service.emq.OrderSendService;
import com.bbzn.device.client.service.emq.order.NbOrderSendStrategy;
import com.bbzn.device.client.utils.SpringUtils;
import com.ctg.ag.sdk.biz.aep_device_command.CreateCommandResponse;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * @Author wulongbo
 * @Date 2021/12/16 15:01
 * @Version 1.0
 */
@Component
public class BaseNbSendImpl implements NbOrderSendStrategy {

    private JSONObject content;

    private RequestCreateCommand requestCreateCommand;

    protected Map map;

    /**
     * 参数校验
     * @param language
     * @return
     * @throws Exception
     */
    @Override
    public String checkMap(String language) throws Exception {
        return JSONObject.toJSONString(map);
    }

    /**
     * 封装
     * @param data
     * @return
     */
    @Override
    public JSONObject createContent(String data) {
        JSONObject content = new JSONObject();
        content.put("payload", data);
        content.put("dataType", 1);
        this.content = content;
        return content;
    }


    /**
     * 下发消息
     * @param productUnionDTO
     */
    @Override
    public void setCreateCommand(ProductUnionDTO productUnionDTO) {
        RequestCreateCommand requestCreateCommand = new RequestCreateCommand();
        requestCreateCommand.setContent(content);
        requestCreateCommand.setDeviceId(productUnionDTO.getNbDeviceId());
        requestCreateCommand.setOperator(productUnionDTO.getUserName());
        requestCreateCommand.setProductId(productUnionDTO.getNBProductId());
        this.requestCreateCommand = requestCreateCommand;
    }

    @Override
    public CreateCommandResponse sendOrder(Map map, ProductUnionDTO productUnionDTO, String language) throws Exception {
        this.map = map;
        String data = checkMap(language);
        JSONObject content = this.createContent(data);
        this.setCreateCommand(productUnionDTO);
        // 使用beanid从容器获取对象
        OrderSendService orderSendService = SpringUtils.getBean(productUnionDTO.getOrderSendStrategy(), OrderSendService.class);
        return orderSendService.createCommand(productUnionDTO.getImei(), productUnionDTO.getMasterKey(), requestCreateCommand, content.toJSONString());
    }
}
